<?php
/**
 * Created by PhpStorm.
 * User: td
 * Date: 15/2/1
 * Time: 上午12:32
 */

namespace Home\Controller;
use Think\Controller;

class MessageController extends Controller {

    //消息首页Index
    public function index(){
        $this->assign('curNav', 2);
        if(isset($_SESSION['loginUser'])){
            session('is_read',2);
            $this->display();
        } else {
            $this->display('Login/index');
        }

    }

    //消息首页Index pc
    public function indexPC(){
        $this->assign('curNav', 2);
        $this->theme('PC')->display('Message/message');
    }



    //消息首页-系统消息
    public function messageSystem(){
        if(isset($_SESSION['loginUser'])){
            if(IS_GET){
                $user_id = $_SESSION['loginUser']['user_id'];
                $message_db = D('Message');
                $message_result = $message_db->getMessageSystem($user_id);
                if($message_result){
                    $this->success('获取信息成功',$message_result);
                    // dump($message_result);
                }else{
                    //获取信息失败
                    $this->error('获取信息失败');
                }
            }
        }else{
            $this->error('请登陆系统',U('Login/index'));
        }
    }

    //消息首页－用户消息
    public function messageUser(){
        if(isset($_SESSION['loginUser'])){
            if(IS_GET){
                $user_id = $_SESSION['loginUser']['user_id'];
                $message_db = D('Message');
                $message_result = $message_db->getMessageUser($user_id);
                if($message_result){
                    $this->success('获取信息成功',$message_result);
                    // dump($message_result);
                }else{
                    //获取信息失败
                    $this->error('获取信息失败');
                }
            }
        }else{
            $this->error('请登录系统',U('Login/index'));
        }
    }

    //收消息
    public function receiveMessage(){
        if(IS_GET){
            $publisher_id = $_GET['publisher_id'];
            //$receiver_id = $_GET['receiver_id'];
            //$publisher_name = $_GET['publisher_name'];
            $receiver_id = $_SESSION['loginUser']['user_id'];
            //dump($receiver_id);
            $message_db = D('Message');
            $message_result = $message_db->getMessageInfo($publisher_id,$receiver_id);

            //消息已读
            $message_data['is_read'] = 2;
            $message_read = $message_db->where(array(array('publisher_id'=>$publisher_id),array('receiver_id'=>$receiver_id)))->save($message_data);

            if(is_array($message_result)){
                $this->success('获取信息成功',$message_result);
            }else{
                $this->error('获取信息失败');
            }
        }else{
            $this->error('错误方法');
        }
    }

    //发消息
    public function sendMessage(){
        if(isset($_SESSION['loginUser'])){
            if(IS_POST){
                //发送信息
                $data['publisher_id'] = $_SESSION['loginUser']['user_id'];
                $data['receiver_id'] = $_POST['publisher_id'];
                $data['content'] = $_POST['content'];
                $data['is_read'] = 1;
                ini_set('date.timezone','Asia/Shanghai');
                $data['time'] = date('Y-m-d H:i:s',time());

                if( NULL == $data['content'] ){
                    $this->error('空内容');
                }else{
                    $message_db = M('Message');
                    $message_result = $message_db->add($data);
                    if($message_result){
                        $this->success('信息提交成功',$message_result);
                    }else{
                        $this->error('信息提交失败');
                    }
                }

            }else{
                $this->error('错误方法');
            }
        }else{
            $this->error('请登陆系统',U('Login/index'));
        }
    }

    //收发消息
    public function sendOrReceiveMessage(){
        if(isset($_SESSION['loginUser'])){
//            if(IS_POST){
//                //发送信息
//                $data['publisher_id'] = $_SESSION['loginUser']['user_id'];
//                $data['receiver_id'] = $_POST['publisher_id'];
//                $data['content'] = $_POST['content'];
//                $data['is_read'] = 1;
//                ini_set('date.timezone','Asia/Shanghai');
//                $data['time'] = date('Y-m-d H:i:s',time());
//
//                $message_db = M('Message');
//                $message_result = $message_db->add($data);
//                if($message_result){
//                    $this->success('信息提交成功',$message_result);
//                }else{
//                    $this->error('信息提交失败');
//                }
//
//            }else{
                //获取信息
                //$publisher_id = $_GET['publisher_id'];
                $publisher_name = $_GET['publisher_name'];
                if( $_SESSION['loginUser']['user_id'] == $_GET['publisher_id'] ){
                    $publisher_id = $_GET['receiver_id'];
                    //dump($publisher_id);
                }else{
                    $publisher_id = $_GET['publisher_id'];
                }
//                $receiver_id = $_SESSION['loginUser']['user_id'];
//                $message_db = D('Message');
//                $message_result = $message_db->getMessageInfo($publisher_id,$receiver_id);
                //dump($message_result);
//                if(is_array($message_result)){
//                    $this->assign('data',$message_result);
                    $this->assign('curNav', 2);
                    $this->assign('publisher_name',$publisher_name);
                    $this->assign('receiver_id', $publisher_id);
                    $this->display('msg_flow');
//                }else{
//                    $this->error('获取消息失败');
//                }

//            }
        }else{
            $this->error('请登录系统',U('Login/index'));
        }
    }

    public function sendOrReceiveMessagePC(){
        if(isset($_SESSION['loginUser'])){
            $publisher_id = $_GET['publisher_id'];
            $publisher_name = $_GET['publisher_name'];
            $this->assign('publisher_name',$publisher_name);
            $this->assign('publisher_id', $publisher_id);
            $this->theme('PC')->display('Message/send');
        }else{
            $this->error('请登录系统',U('Login/index'));
        }
    }

    //test
    public function test(){
        $user_id = $_GET['user_id'];
        $message_db = D('Message');
//        $message_result1 = $message_db->getMessageSystem($user_id);
//        dump($message_result1);
        $message_result2 = $message_db->getMessageUser($user_id);
        dump($message_result2);

//        $publisher_id = $_GET['publisher_id'];
//        $receiver_id = $_GET['receiver_id'];
//        $message_db = D('Message');
//        $message_result = $message_db->getMessageInfo($publisher_id,$receiver_id);
//        dump($message_result);
    }

} 